<template>
  <div class="container">
    <el-form v-if="showRegiste" ref="registeForm" :model="registeForm" :rules="registeRules" class="form" autocomplete="on" label-position="left">

      <div v-if="showVerifyEmail" class="verify-email">
        <div v-if="show2Pwd" class="pwd-box">
          <div class="title-container">
            <h3 class="title">设置密码</h3>
          </div>

          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="registeForm.password"
              :type="passwordType"
              placeholder="Password"
              name="password"
              tabindex="2"
              autocomplete="on"
              @keyup.enter.native="handleRegiste"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>

          <el-form-item prop="password_confirm">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password_confirm"
              v-model="registeForm.password_confirm"
              :type="passwordType"
              placeholder="Confirm password"
              name="password_confirm"
              tabindex="2"
              autocomplete="on"
              @keyup.enter.native="handleRegiste"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>

          <el-button type="primary" style="width:100%;" @click.native.prevent="handleRegiste">完成</el-button>
        </div>
        <div v-else class="code-box">
          <div class="title-container">
            <h3 class="title">选择公司</h3>
          </div>
          <el-form-item prop="code" style="margin:30px 0;">
            <span class="svg-container">
              <svg-icon icon-class="code" />
            </span>
            <el-select v-model="registeForm.department" placeholder="请选择所属公司" style="width:90%;">
              <el-option
                v-for="item in departmentOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <div class="subtitle-container">
            <span @click="show2Pwd = true;registeForm.code = -1">跳过</span>
            <span @click="showRegiste = false">已有账号，去登录</span>
          </div>
          <el-button type="primary" style="width:100%;margin-top:30px;" @click.native.prevent="handleContinue">下一步</el-button>
        </div>
      </div>

      <div v-else class="registe-box">
        <div class="logo-container">
          <img :src="require('@/assets/logo.png')" alt="" srcset="">
          <section class="lpcsoft">
            <el-badge value="beta" class="badge-item">
              <h2>LPCSOFT</h2>
            </el-badge>
            <p>1.0.49</p>
          </section>
        </div>

        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="registeForm.username"
            placeholder="Username"
            name="username"
            type="text"
            tabindex="1"
            autocomplete="on"
          />
        </el-form-item>

        <div class="subtitle-container">
          <span />
          <span @click="showRegiste = false">已有账号，去登录</span>
        </div>

        <el-button :loading="loading" type="primary" style="width:100%;margin-top:30px;" @click.native.prevent="handleSend">下一步</el-button>
      </div>

    </el-form>

    <el-form v-else ref="loginForm" :model="loginForm" :rules="loginRules" class="form" autocomplete="on" label-position="left">

      <div class="title-container">
        <div class="logo-container">
          <img :src="require('@/assets/logo.png')" alt="" srcset="">
          <section class="lpcsoft">
            <el-badge value="beta" class="badge-item">
              <h2>LPCSOFT</h2>
            </el-badge>
            <p>1.0.49</p>
          </section>
        </div>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          autocomplete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <!-- <div class="subtitle-container">
        <span>注册请联系管理员</span>
        <span>inpm.cy@gmail.com</span>
        <span @click="showRegiste = true">注册</span>
        <span @click="showRegiste = true">忘记密码？</span>
      </div> -->

      <!-- <div class="login-way-container">
        <svg-icon icon-class="wechat" class="login-way-icon" style="color:#30B08F;" />
        <svg-icon icon-class="qq" class="login-way-icon" style="color:#3A71A8;" />
        <svg-icon icon-class="github" class="login-way-icon" />
      </div> -->

      <el-button :loading="loading" type="primary" style="width:100%;" @click.native.prevent="handleLogin">登录</el-button>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码最少6位'))
      } else {
        callback()
      }
    }
    const validatePassword2 = (rule, value, callback) => {
      if (value !== this.registeForm.password) {
        callback(new Error('两次密码不相同'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      registeForm: {
        username: '',
        department: '律品',
        password: '',
        password_confirm: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      registeRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        password_confirm: [{ required: true, trigger: 'blur', validator: validatePassword2 }]
      },
      passwordType: 'password',
      departmentOptions: [{
        value: '律品',
        label: '律品'
      }],
      loading: false,
      redirect: undefined,
      otherQuery: {},
      showRegiste: false,
      showVerifyEmail: false,
      show2Pwd: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      if (!this.showRegiste) {
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              this.$message.success('登录成功！')
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleSend() {
      this.$refs.registeForm.validate(valid => {
        if (valid) {
          console.log('Send code', valid)
          this.showVerifyEmail = true
          // sendCode().then(() => {
          // })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleContinue() {
      this.$refs.registeForm.validate(valid => {
        if (valid) {
          console.log('Varify pass', valid)
          this.show2Pwd = true
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleRegiste() {
      this.$refs.registeForm.validate(valid => {
        if (valid) {
          console.log('Pwd2 pass', valid)
          this.loading = true
          this.$store.dispatch('user/register', this.registeForm)
            .then(() => {
              this.$message.success('注册成功！')
              this.loading = false
              this.showRegiste = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      // &:-webkit-autofill {
      //   box-shadow: 0 0 0px 1000px $bg inset !important;
      //   -webkit-text-fill-color: $cursor !important;
      // }
    }

    /* 改变input框背景颜色 */
    input:-webkit-autofill , textarea:-webkit-autofill, select:-webkit-autofill {
      -webkit-text-fill-color: #ededed !important;
      -webkit-box-shadow: 0 0 0px 1000px transparent  inset !important;
        background-color:transparent;
        background-image: none;
      /* //背景色透明  生效时长  过渡效果  启用时延迟的时间 */
      transition: background-color 50000s ease-in-out 0s;
    }
    input {
      background-color:transparent;
    }
    input:-webkit-autofill {
      /* //transparent 即设置背景色为透明-可改为其他颜色 */
      -webkit-box-shadow: 0 0 0px 1000px transparent inset !important;
      -webkit-text-fill-color: rgba(255,255,255,1)!important;
    }
  }

  .logo-container {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 50px;
    img {
      margin-right: 20px;
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#282c34;
$dark_gray:#889aa4;
$light_gray:#eee;

.container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 360px 35px 0;
    padding-top: 10%;
    margin: 0 auto;
    overflow: hidden;
  }

  .subtitle-container {
    display: flex;
    justify-content: space-between;
    span {
      font-size: 14px;
      color: #fff;
      cursor: pointer;
    }
  }

  .verify-email {
    text-align: center;
    color: #fff;
  }

  .login-way-container {
    display: flex;
    justify-content: space-around;
    margin: 40px 0;
    .login-way-icon {
      font-size: 30px;
      cursor: pointer;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .send-code-container {
    cursor: pointer;
    margin-right: -10px;
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }

  .lpcsoft {
    color: #fff;
    p {
      margin: 0;
    }
  }

  .badge-item /deep/ .el-badge__content {
    background:#1f1d2f;
  }
}
</style>
